<!-- 这个模块我正在开发 我是qq是1227971437   昵称 界面开发-董连朔  -->
<template>
	<view class="page">
		<view class="address-box add-box">
			<text class="label label-img">消毒照片</text>
			<view class="add-img-box">
				<view class="add-img-item" v-for="(item, index) in imgList" :key="index">
					<image class="add-img" @click="imgInfo(index)" :src="item.path" mode="aspectFill"></image>
					<image v-if="!disinfectId" class="add-img-del" @click="delImg(index)" src="../../../static/delete.png"></image>
				</view>
				<view v-if="imgList.length < 9 && !disinfectId" class="add-img-item" @click="openCamera"><image class="add-img" src="../../../static/add.png"></image></view>
			</view>
		</view>
		<!-- <uni-combox label="消毒位置"   :candidates="candidates"
				placeholder="请选择" v-model="addressTxt"
			></uni-combox> -->
		<!-- <picker class="address-box" :value="addressTxt" :range="candidates">
		</picker> -->
		<view class="address-box" v-if="user_name">
			<text class="label">消毒位置</text>
			<text>{{addressTxt}}</text>
		</view>
		
		<view v-if="!user_name" class="line"></view>
		<view class="address-box" v-if="!user_name">
			<text class="label">消毒位置</text>
			<picker-view style="height: 100px;width: 500rpx;" @change="choseAddress">
				<picker-view-column>
					<view v-for="(item,index) in  candidates" :key="index">
						{{item}}
					</view>
				</picker-view-column>
			</picker-view>
		</view>
		<view v-if="!user_name" class="line"></view>
		
		<view class="address-box" v-if="user_name">
			<text class="label">工作人员</text>
			<text>{{user_name}}</text>
		</view>
		<view class="address-box" v-if="user_name">
			<text class="label">上报时间</text>
			<input type="text" @blur="changeAddress" :disabled="true" class="address-time" v-model="addTime" placeholder="获取时间" />
		</view>
		<view v-if="!disinfectId" @click="submit" class="address-box submit-box"><text class="submit-btn">提交</text></view>
	</view>
</template>

<script>
import uniNavBar from '@/components/uni-nav-bar/uni-nav-bar.vue';
import myCloud from '@/common/app.js';
export default {
	components: {
		uniNavBar
	},
	data() {
		return {
			addTime: '',
			addTimeNumber: '',
			addressTxt: '',
			lon: '0',
			lat: '0',
			covers: [],
			imgList: [],
			imgCount: 9,
			submitData: null,
			disinfectId: false,
			pageTitle:'消毒详情',
			candidates:[],
			user_name:false
		};
	},
	onLoad(e) {
		// this.disinfectId = '5e3970b1d084d0004f18fff3';
		this.disinfectId = e.id?(e.id):false;
		if(this.disinfectId){
			this.pageTitle = '消毒详情';
			this.getInfo(this.disinfectId);
		}else{
			this.pageTitle = '消毒登记';
			this.getLocation();
			this.getTime();
		}
		myCloud.callFunction('candidates_location','', (data, e) => {
			console.log(data,'candidates_location-')
			this.candidates = data;
			this.addressTxt = data[0];
		})
	},
	methods: {
		changeAddress(e) {
			this.addressTxt = e.detail.value;
		},
		submit(e) {
			this.submitData = null;
			let tempImgList = [];
			if (this.addressTxt.length < 1) {
				uni.showToast({
					title: '请填写你的消毒位置',
					icon: 'none'
				});
				return false;
			}
			if (this.imgList.length < 1) {
				uni.showToast({
					title: '请添加消毒图片',
					icon: 'none'
				});
				return false;
			}
			if (this.addTime.length < 1) {
				uni.showToast({
					title: '请获取时间',
					icon: 'none'
				});
				return false;
			}
			let tempLocation = {
				longitude: this.lon || '0',
				latitude: this.lat || '0',
				address: this.addressTxt
			};
			let tempTime = {
				text: this.addTime,
				time: this.addTimeNumber
			};
			uni.showLoading({
				title: '上传图片',
				mask: false
			});
			for (let i = 0; i < this.imgList.length; i++) {
				myCloud.uploadFile(this.imgList[i].path, (res)=>{
					tempImgList.push(res.fileID);
					if (tempImgList.length == this.imgList.length) {
						uni.hideLoading();
						let tempData = {
							disinfect_location: {
								longitude: this.lon || '0',
								latitude: this.lat || '0',
								address: this.addressTxt
							},
							creat_time: {
								text: this.addTime,
								time: this.addTimeNumber
							},
							disinfect_img: tempImgList
						};
						this.submitData = tempData;
						this.add_disinfect();
					}
				})
			}
		},
		getInfo(id) {
			myCloud.callFunction('select_disinfect', {
				id:id
			},(res)=>{
				let data = res[0];
				this.lon = data.disinfect_location.longitude;
				this.lat = data.disinfect_location.latitude;
				this.addressTxt = data.disinfect_location.address;
				this.covers = [
					{
						id: 1,
						title: '我在这里',
						latitude: data.disinfect_location.longitude,
						longitude: data.disinfect_location.latitude
					}
				];
				this.addTime = data.creat_time.text;
				this.addTimeNumber = data.creat_time.time;
				this.user_name = data.user_name
				this.imgList = data.disinfect_img.map(img=>{
					return {
						path:img
					}
				})
			});
		},
		add_disinfect() {
			myCloud.callFunction('add_disinfect', {
				data:this.submitData
			}, (res)=>{
				console.log(res);
				uni.showToast({
					title: '提交成功',
					success:()=>{
						uni.navigateBack()
					}
				});
			})
		},
		imgInfo(i) {
			let tempList = [];
			this.imgList.forEach(img => {
				tempList.push(img.path);
			});
			uni.previewImage({
				current: i,
				loop: false,
				urls: tempList,
				indicator: 'default'
			});
		},
		delImg(i) {
			uni.showModal({
				content: '确定删除这张吗',
				success: res => {
					if (res.confirm) {
						this.imgList.splice(i, 1);
						this.imgCount++;
					} else if (res.cancel) {
					}
				}
			});
		},
		openCamera() {
			uni.chooseImage({
				sourceType:['camera'],
				count: this.imgCount,
				sizeType: ['compressed'],
				success: e => {
					this.imgList = [...this.imgList, ...e.tempFiles];

					this.imgCount = 9 - this.imgList.length;
					console.log('imgCount', this.imgCount);
				}
			});
		},
		getTime() {
			let currentTime = new Date();
			let m = currentTime.getMonth() + 1;
			let d = currentTime.getDate();
			let hh = currentTime.getHours();
			let mm = currentTime.getMinutes();
			let ss = currentTime.getSeconds();
			this.addTimeNumber = currentTime.getTime();
			this.addTime =
				currentTime.getFullYear() + '年' + (m > 9 ? m : '0' + m) + '月' + (d > 9 ? d : '0' + d) + '日 ' + (hh > 9 ? hh : '0' + hh) + ':' + (mm > 9 ? mm : '0' + mm);
		},
		back() {
			uni.navigateBack();
		},
		choseAddress(e){
			console.log(e.target.value)
			this.addressTxt = this.candidates[e.target.value]
		},
		getLocation() {
			this.lon = '0';
			this.lat = '0';
			this.addressTxt = '';
			uni.getLocation({
				geocode: true,
				success: res => {
					console.log(res);
					let id = new Date().getTime();
					this.covers = [
						{
							id: id,
							title: '我在这里',
							latitude: res.longitude,
							longitude: res.latitude
						}
					];
					//this.addressTxt = res.address.city + res.address.district + res.address.poiName + res.address.province + res.address.street + res.address.streetNum;
					this.lon = res.longitude;
					this.lat = res.latitude;
				},
				complete: () => {
					
				}
			});
		},
		openLocation() {
			uni.navigateTo({
				url: './disinfect_map'
			});

			return false;
			console.log('openLocation');
			uni.getLocation({
				success(res) {
					// console.log(res);
					uni.openLocation({
						longitude: res.longitude,
						latitude: res.latitude
					});
				}
			});
		}
	}
};
</script>

<style lang="scss">
.page{
	width: 750rpx;
	height: 100vh;
	overflow: hidden;
}
.back {
	font-size: 32rpx;
	font-weight: 500;
	line-height: 48rpx;
	color: rgba(153, 153, 153, 1);
}
.back-hover {
	opacity: 0.4;
}
.submit-box {
	position: fixed;
	bottom: 0;
	left: 0;
	display: flex;
	width: 750rpx;
	justify-content: center;
}
.submit-btn {
	margin-top: 10px;
	display: inline-block;
	width: 670rpx;
	height: 96rpx;
	line-height: 96rpx;
	text-align: center;
	background-color: rgba(109, 221, 203, 1);
	opacity: 1;
	border-radius: 32rpx;
	border-width: 8rpx;
	border-color: rgba(255, 255, 255, 1);
	box-shadow: 0rpx 8rpx 12rpx rgba(0, 0, 0, 0.16);
	border-radius: 56rpx;
	font-size: 39rpx;
	font-weight: bold;
	color: rgba(255, 255, 255, 1);
}
.submit-btn-txt {
	font-size: 39rpx;
	font-weight: bold;
	line-height: 47rpx;
	color: rgba(255, 255, 255, 1);
	opacity: 1;
}
.map-box {
	width: 484rpx;
	height: 256rpx;
	border-width: 4rpx;
	border-color: rgba(255, 255, 255, 1);
	box-shadow: 0rpx 0rpx 24rpx rgba(0, 0, 0, 0.16);
	/* border-radius: 12rpx; */
	position: relative;
}
.map {
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	width: 476rpx;
	height: 250rpx;
}
.map-img {
	position: absolute;
	top: 90rpx;
	left: 156rpx;
	width: 230rpx;
	height: 68rpx;
	background-color: rgba(51, 51, 51, 0.64);
	border-width: 1rpx;
	border-color: rgba(0, 0, 0, 0);
	border-radius: 34px;

	font-size: 28rpx;
	font-weight: bold;
	line-height: 66rpx;
	color: rgba(255, 255, 255, 1);
	text-align: center;
}
.address-box {
	display: flex;
	width: 670rpx;
	flex-direction: row;
	align-items: center;
	justify-content: space-between;
	padding: 15rpx 40rpx;
	margin-bottom: 10px;
}
.label {
	font-size: 36rpx;
	font-weight: bold;
	line-height: 50rpx;
	color: #222222;
}
.label-img {
	padding-left: 40rpx;
}
.address-box-txt {
	align-items: flex-start;
}
.address-txt {
	width: 484rpx;
	height: 90rpx;
	font-size: 32rpx;
	font-weight: 500;
	line-height: 45rpx;
	color: rgba(51, 51, 51, 1);
}
.add-box {
	padding: 15rpx 0;
	flex-wrap: wrap;
}
.add-img-box {
	display: flex;
	width: 750rpx;
	//padding: 0 40rpx;
	padding-left: 10rpx;
	flex-direction: row;
	flex-wrap: wrap;
}
.add-img-item {
	/* width:210rpx;
		height:210rpx; */
	width: 200rpx;
	height: 200rpx;
	border-radius: 24rpx;
	position: relative;
	padding: 9rpx 0;
	margin-left: 20rpx;
}
.add-img-camera {
	flex: 1;
}
.add-img {
	width: 200rpx;
	height: 200rpx;
	border-radius: 24rpx;
}
.add-img-del {
	position: absolute;
	width: 40rpx;
	height: 40rpx;
	right: 8rpx;
	top: 17rpx;
	//background-color: rgba(238, 0, 0, 1);
	border-radius: 20rpx;
}
.address-time {
	width: 484rpx;
	height: 88rpx;
	background-color: rgba(245, 245, 245, 1);
	opacity: 1;
	border-radius: 24rpx;
	text-align: center;

	font-size: 35rpx;
	font-weight: 500;
	color: rgba(51, 51, 51, 1);
}
.line{
	width: 750rpx;
	height: 1px;
	transform: scaleY(0.3);
	background-color: rgba(0,0,0,0.5);
}
</style>
